import javax.swing.*;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * Created by ospip on 2017/11/29.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
public class UserQueue {
    private PriorityBlockingQueue<User> q;
    private char queueName;
    private String comment = "";
    private int count = 1;

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    private String group;

    public UserQueue(char name, String comment) {
        q = new PriorityBlockingQueue<>(128, new UserComparator());
        queueName = name;
        this.comment = comment;
    }

    public UserQueue(int no, String comment) {
        this((char)('A' + no), comment);
    }

    public UserQueue(int no) {
        this(no, "");
    }

    public UserQueue(char name) {
        this(name, "");
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public void setName(char queueName) {
        this.queueName = queueName;
    }

    public String getComment() {
        return this.comment;
    }

    public int getSize() {
        return q.size();
    }

    public char getQueueName() {
        return queueName;
    }

    public void setQueueName(char queueName) {
        this.queueName = queueName;
    }

    public String insert(int p) {
        if (p < 0 || p > 9) return "Null";
        User u = new User(String.format("%C%1d%03d", queueName, p, count++));
        q.put(u);
//        System.out.println(u.getName());
        return u.getName();
    }

    public String insert() {
        return this.insert(0);
    }

    public synchronized User poll() {
        if (q.size() == 0) {
            return null;
        }
        return q.poll();
    }

    public void output() {
        System.out.println("Size: " + q.size());
        User t;
        while((t = q.poll()) != null) System.out.println(t);
    }

    public static void main(String[] args) {
        int c = 0;
        UserQueue q = new UserQueue(c++);
        q.insert();
        q.insert();
        q.insert(0);
        q.insert(1);
        q.output();
    }
}
